﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
   public  class Achievements
    {
        public bool previouslocked; //Delock une seule fois
        public bool locked; //Le succès est-il ou non déverouillé?
        public  string name; //Nom [x], description [x+1]
        public string desc;
        public int limit; // Nombre de fois qu'on dois répter l'action pour faire le succès.
        public int count; // Le compteur pour limit
        public Button cadre = new Button(new Vector2(250, - 30),GameTexture.Ach_cadre);
       



        public Achievements(string name_, string desc_, int limit_)
        {
            this.locked = true;
            this.name = name_;
            this.desc = desc_;
            this.limit = limit_;
            this.count = 0;
        }

        public void Add()
        {
            if (locked)
            {
                previouslocked = locked;
                count++;
                if (count >= limit)
                    locked = false;

                if (!locked && previouslocked != locked)
                {
                    User.AchievertList.Add(new Achievert(this));
                    GameSound.AchievLocked.Play(0.4f,0,0);
                    AchievementManager.nberofunlocked++;
                }

                if (locked && limit > 2 && count == (int)(limit / 2))
                    User.AchievertList.Add(new Achievert(this));

                AchievementManager.AchievementSave();
            }
        }

        public void Add(int add)
        {
            if (locked)
            {
                previouslocked = locked;
                count += add;
                if (count >= limit)
                {
                    locked = false;
                    count = limit;
                }

                if (!locked && previouslocked != locked)
                {
                    User.AchievertList.Add(new Achievert(this));
                    GameSound.AchievLocked.Play(0.4f, 0, 0);
                }

                if (locked && limit > 2 && count >= (int)(limit / 2) && count - add < (int)(limit / 2))
                    User.AchievertList.Add(new Achievert(this));
            }
        }

        public void Reset()
        {
            count = 0;
            locked = true;
        }

        public void Draw(SpriteBatch spriteBatch, int y)
        {
            cadre.Position = new Vector2(580, y + 15);
            if (locked)
                cadre.Texture = GameTexture.Ach_cadre;
            else cadre.Texture = GameTexture.Ach_cadreun;
            cadre.Draw(spriteBatch);
            spriteBatch.DrawString(GameData.Font_Achiev2, name, new Vector2(270, y -17), Color.Violet);
            spriteBatch.DrawString(GameData.Font_Achiev2, desc, new Vector2(270, y +5), Color.Orange);
            if (limit>1)
            spriteBatch.DrawString(GameData.Font_Achiev2, count + " / " + limit, new Vector2(700, y + 30), Color.Azure);
            if (locked)
            spriteBatch.DrawString(GameData.Font_Achiev,"Locked", new Vector2(700, y - 15), Color.Yellow);
            else spriteBatch.DrawString(GameData.Font_Achiev, "Unlocked", new Vector2(700, y - 15), Color.LimeGreen);

            if (limit > 1)
            {
                Texture2D bar = GameTexture.ExperienceBar;
                spriteBatch.Draw(bar, new Rectangle(577, y + 15, 300, 15), Color.White);
                spriteBatch.Draw(GameTexture.Experience, new Rectangle(577, y + 15, (int)(((float)count / (float)limit) * 300), 15), Color.LimeGreen);
            }
        }
    }
}